package com.xujianlun.cpms.controller.shop;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xujianlun.cpms.common.Response;
import com.xujianlun.cpms.config.AuthAccess;
import com.xujianlun.cpms.mapper.shop.GoodsMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xujianlun.cpms.service.shop.IGoodsService;
import com.xujianlun.cpms.entity.shop.Goods;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xujianlun
 * @since 2022-12-13
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private IGoodsService goodsService;
    @Resource
    private GoodsMapper goodsMapper;
    // 新增或者更新
    @PostMapping
    public Response save(@RequestBody Goods goods) {
        if (goods.getId()==null){
            goods.setTime(DateUtil.now());
        }
        goodsService.saveOrUpdate(goods);
        return Response.success();
    }
    //启用或者不启用
    @PostMapping("/status")
    public Response update(@RequestBody Goods goods) {
        goodsMapper.updateById(goods);
        return Response.success();
    }
    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Integer id) {
        goodsService.removeById(id);
        return Response.success();
    }

    @PostMapping("/batch")
    public Response deleteBatch(@RequestBody List<Integer> ids) {
        goodsService.removeByIds(ids);
        return Response.success();
    }

    @GetMapping
    public Response findAll() {
        return Response.success(goodsService.list());
    }

    @AuthAccess
    @GetMapping("/{id}")
    public Response findOne(@PathVariable Integer id) {
        return Response.success(goodsService.getById(id));
    }

    @GetMapping("/page")
    public Response findPage(@RequestParam Integer pageNum,
                             @RequestParam(defaultValue = "") String name,
                             @RequestParam Integer pageSize) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        if (!"".equals(name)){
            queryWrapper.like("name",name);
        }
        queryWrapper.orderByDesc("id");
        return Response.success(goodsService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 展示上架的商品给前台用户
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @AuthAccess
    @GetMapping("/findGoods")
    public Response findGoods(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam(defaultValue = "") String name) {
        Integer status=1;
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        if (!"".equals(name)){
            goodsQueryWrapper.like("name",name);
        }
        goodsQueryWrapper.eq("status",status);
        goodsQueryWrapper.orderByDesc("id");
        return Response.success(goodsService.page(new Page<>(pageNum, pageSize), goodsQueryWrapper));
    }
}

